import React, { useContext, useEffect, useMemo, useState } from 'react';
import Cls from 'classnames';
import { Empty, Table } from 'antd';
import { ChangeControllerContext } from '../common/useOAUseContext';
import { IUseOAActionEnum } from '../common/useOAUseReducer';
import { getLoadMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { useAssetTableColumn } from '../mconfig/AssetTablecolumn';
import { useOAAssetTableColumn } from '../mconfig/OATableColumns';

/** 领用OA-- 二、资产列表 */
const DetailInfoAssetList: React.FC<any> = ({ dataList, openAssetModalFun, changeEditAssetId, finish }) => {
  /** 引入上下文 */
  const { assetList, isEdit, infoObj, useOADispatch } = useContext(ChangeControllerContext);

  /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.ASSET_AREA}`, //资产存放地点
  ]);

  /** 取消选择的资产 */
  const delSelectRow = (record) => {
    const arr = assetList.filter((item) => {
      return item.id !== record.id;
    });
    useOADispatch({
      type: IUseOAActionEnum.SELECT_ASSET,
      payload: arr,
    });
  };

  const isEditMemo = useMemo(() => {
    if (isEdit) {
      return 'edit';
    } else {
      return 'detail';
    }
  }, [isEdit]);

  const columns: Record<string, any>[] = useMemo(() => {
    return useOAAssetTableColumn({
      pageType: isEditMemo,
      openAssetModalFun,
      delSelectRow,
      mapLoading,
      loadMapInfo,
      editState: isEdit,
      finish,
      changeEditAssetId,
    });
  }, [assetList, loadMapInfo, isEdit, mapLoading, finish]);

  return (
    <div style={{ width: '100%' }}>
      <div className={Cls('list-table', 'UpdatePackTable')}>
        {isEdit && <Table scroll={{ x: 1200, y: 160 }} bordered size="small" dataSource={assetList} columns={columns} rowKey={'id'} pagination={false} />}
        {!isEdit &&
          (assetList.length > 0 ? (
            <Table scroll={{ x: 1200, y: 160 }} bordered size="small" dataSource={assetList} columns={columns} rowKey={'id'} pagination={false} />
          ) : (
            <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
          ))}
      </div>
    </div>
  );
};

export default DetailInfoAssetList;
